package com.westar.base.validateutil;

import com.westar.base.emum.PublicServiceEnum;
import com.westar.base.exceptions.ValidateException;
import com.westar.base.pojo.Affair;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class ValidateEditAffair {

    /**
     * 验证参数的正确性
     * @param affair
     * @return
     */
    public static void checkForAddAffair( Affair affair) throws ValidateException {
        String eventCode = affair.getEventCode();
        if(StringUtils.isEmpty( eventCode)){
            throw new ValidateException(-1,"事项编码:eventCode不能为空","");
        }

        String isNotPublicServices = affair.getIsNotPublicServices();
        if(StringUtils.isNotEmpty(isNotPublicServices)
            && null == PublicServiceEnum.getEnum(isNotPublicServices)){
            throw new ValidateException(-1,"isNotPublicServices：参数为power 、 service 或 空","");
        }

        //1.现场取件  2.邮寄送达  3.无需取件
        String pickupTypeStr = affair.getPickupType();
        if(StringUtils.isEmpty(pickupTypeStr)){
            throw new ValidateException(-1," pickupType :参数不能为空1.现场取件2.邮寄送达3.无需取件","");
        }
        Integer pickupType =  Integer.parseInt(pickupTypeStr);
        if(pickupType>4 || pickupType < 1){
            throw new ValidateException(-1," pickupType :1.现场取件2.邮寄送达3.无需取件","");
        }
        if(pickupType.equals( 2 )){
            String recPhone = affair.getRecPhone();
            if(StringUtils.isEmpty(recPhone)){
                throw new ValidateException(-1,"未填写取件人联系电话！recPhone","");
            }

            String postalCode =affair.getPostalCode();
            if(StringUtils.isEmpty(postalCode)){
                throw new ValidateException(-1,"未填写取件人邮政编码！postalCode","");
            }
        }

        //1.企业法人 2.自然人 3.其他组织 4.事业法人5.社会组织法人 6.行政机关 7.非法人企业
        String applicantTypeStr = affair.getApplicantType();
        if(StringUtils.isEmpty(applicantTypeStr)){
            throw new ValidateException(-1," applicantType:参数不能为空! 1.企业法人 2.自然人 3.其他组织 4.事业法人5.社会组织法人 6.行政机关 7.非法人企业","");
        }
        Integer applicantType = Integer.parseInt(applicantTypeStr);
        if(applicantType > 8 || applicantType < 1 ){
            throw new ValidateException(-1," applicantType:1.企业法人 2.自然人 3.其他组织 4.事业法人5.社会组织法人 6.行政机关 7.非法人企业","");
        }

        //联系人性别1.男 2.女
        String contactSex = affair.getContactSex();
        if((!"1".equals(contactSex))&& (!"2".equals(contactSex))){
            throw new ValidateException(-1," contactSex:1.男 2.女","");
        }

        //证件类型
        String cardType = affair.getCardType();
        String[] cardTypes = new String[]{"113","413","418","118","792","111","411","511","117","516","553","115","415","114","119","791","112","412","555","554","154","414","120","122"};
        String[] companyCardTypes = new String[]{"1","2","3","4","5","830","821","6","813","99"};
        if(2==applicantType){
            if(!Arrays.asList(cardTypes).contains(cardType)){
                throw new ValidateException(-1," cardType:113.户口簿 413.公务普通护照 418.外国人旅行证 118.中国人民解放军士兵证 792.境外人员身份证明 111.居民身份证 411.外交护照 511.台湾居民来往大陆通行证 117.出生医学证明 516.港澳居民来往内地通行证 553.外国人永久居留身份证 115.中国人民武装警察部队警官证 415.旅行证 114.中国人民解放军军官证 119.中国人民武装警察部队士兵证 791.外交人员身份证明 112.临时居民身份证 412.公务护照 555.外国人临时居留证 554.外国人居留证或居留许可 154.居住证 414.普通护照 120.中国人民解放军文职人员证 122.中国人民武装警察部队文职人员证","");
            }
        }else {
            if(!Arrays.asList(companyCardTypes).contains(cardType)){
                throw new ValidateException(-1," cardType类型错误： "+cardType);
            }
        }

        //自然人
        if (applicantType == 2) {
            //申请人性别1.男 2.女
            String applicantsex = affair.getApplicantSex();
            if((!"1".equals(applicantsex))&& (!"2".equals(applicantsex))){
                throw new ValidateException(-1," applicantsex:1.男 2.女","");
            }

            if(StringUtils.isEmpty(affair.getApplicantName())){
                throw new ValidateException(-1,"未填写自然人姓名！applicantName","");
            }
            if(StringUtils.isEmpty(affair.getApplicantSex())){
                throw new ValidateException(-1,"未填写自然人性别！applicantSex","");
            }
            if(StringUtils.isEmpty(affair.getCardType())){
                throw new ValidateException(-1,"未填写证件类型！cardType","");
            }
            if(StringUtils.isEmpty(affair.getApplicantIdCard())){
                throw new ValidateException(-1,"未填写身份证号！applicantIdCard","");
            }
            if(StringUtils.isNotEmpty(affair.getApplicantPostalCode())&&affair.getApplicantPostalCode().length() > 6){
                throw new ValidateException(-1,"申请人邮编不能大于6位！applicantPostalCode","");
            }
            if(StringUtils.isNotEmpty(affair.getContractPostalCode())&&affair.getContractPostalCode().length() > 6){
                throw new ValidateException(-1,"联系人邮编不能大于6位！contractPostalCode","");
            }
        }else if (applicantType == 1) { //法人
            if(StringUtils.isEmpty(affair.getMainProperty())){
                throw new ValidateException(-1,"未填写主体性质！mainProperty","");
            }
            if(StringUtils.isEmpty(affair.getCardType())){
                throw new ValidateException(-1,"未填写证件类型！cardType","");
            }
            if(StringUtils.isEmpty(affair.getApplicantName())){
                throw new ValidateException(-1,"未填写机构名称！applicantName","");
            }
            if(StringUtils.isEmpty(affair.getLegalPerson())){
                throw new ValidateException(-1,"未填写法定代表人！legalPerson","");
            }
            if(StringUtils.isNotEmpty(affair.getApplicantPostalCode())&&affair.getApplicantPostalCode().length() > 6){
                throw new ValidateException(-1,"申请人邮编不能大于6位！applicantPostalCode","");
            }
            if(StringUtils.isNotEmpty(affair.getContractPostalCode())&&affair.getContractPostalCode().length() > 6){
                throw new ValidateException(-1,"联系人邮编不能大于6位！contractPostalCode","");
            }
        }else  if (applicantType == 3) { //其他组织
            if(StringUtils.isEmpty(affair.getMainProperty())){
                throw new ValidateException(-1,"未填写主体性质！mainProperty","");
            }
            if(StringUtils.isEmpty(affair.getCardType())){
                throw new ValidateException(-1,"未填写证件类型！cardType","");
            }
            if(StringUtils.isEmpty(affair.getApplicantIdCard())){
                throw new ValidateException(-1,"未填写证件号！applicantIdCard","");
            }
            if(StringUtils.isEmpty(affair.getApplicantName())){
                throw new ValidateException(-1,"未填写机构名称！applicantName","");
            }
            if(StringUtils.isEmpty(affair.getLegalPerson())){
                throw new ValidateException(-1,"未填写法定代表人！legalPerson","");
            }
            if(StringUtils.isNotEmpty(affair.getApplicantPostalCode())&&affair.getApplicantPostalCode().length() > 6){
                throw new ValidateException(-1,"申请人邮编不能大于6位！applicantPostalCode","");
            }
            if(StringUtils.isNotEmpty(affair.getContractPostalCode())&&affair.getContractPostalCode().length() > 6){
                throw new ValidateException(-1,"联系人邮编不能大于6位！contractPostalCode","");
            }
        }
        if (applicantType != 2) {
            if(StringUtils.isEmpty(affair.getLegalpersonIdCard())){
                throw new ValidateException(-1,"未填写法定代表人身份证号！legalpersonIdCard","");
            }
        }
        //联系人
        if(StringUtils.isEmpty(affair.getContactName())){
            throw new ValidateException(-1,"未填写联系人姓名！contactName","");
        }
        if(StringUtils.isEmpty(affair.getContactSex())){
            throw new ValidateException(-1,"未填写联系人性别！contactSex","");
        }
        if(StringUtils.isEmpty(affair.getContactIdCard())){
            throw new ValidateException(-1,"未填写联系人身份证号！contactIdCard","");
        }
    }
}
